package com.softtown.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.oss.cloud.OSSFactory;
import com.softtown.modules.sys.dao.SysFeedBackReplyDao;
import com.softtown.modules.sys.dto.SysFeedBackReplyDTO;
import com.softtown.modules.sys.entity.SysFeedBackReplyEntity;
import com.softtown.modules.sys.enums.FbHasAttDocEnum;
import com.softtown.modules.sys.enums.FbIsReadEnum;
import com.softtown.modules.sys.enums.UserTypeEnum;
import com.softtown.modules.sys.service.SysFeedBackReplyService;
import com.softtown.modules.sys.service.SysFeedBackService;
import com.softtown.modules.sys.vo.SysFeedBackReplyVO;

import java.util.Objects;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: SysFeedBackReplyServiceImpl
 * @Description: 用户反馈回复Service实现层
 * @Author: fuming
 * @Date: 2019/7/1 16:13
 */
@Service
public class SysFeedBackReplyServiceImpl extends
        BaseServiceImpl<SysFeedBackReplyDao, SysFeedBackReplyEntity> implements
        SysFeedBackReplyService {

    @Resource
    private SysFeedBackService sysFeedBackService;

    /**
     * @param @param [sysFeedBackReplyDTO]
     * @return @return void
     * @throws
     * @Title: addFeedbackReply
     * @Description: 添加反馈回复Service实现方法
     * @author fuming
     * @date 2019/7/1 21:32
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addFeedbackReply(SysFeedBackReplyDTO sysFeedBackReplyDTO) {
        SysFeedBackReplyEntity sysFeedBackReplyEntity = ConvertUtils
                .sourceToTarget(sysFeedBackReplyDTO, SysFeedBackReplyEntity.class);
        sysFeedBackReplyEntity.setUserType(UserTypeEnum.GENERAL_USER.value());
        sysFeedBackReplyEntity.setReplyHasAttDoc(FbHasAttDocEnum.NO.value());
        sysFeedBackReplyEntity.setReplyIsRead(FbIsReadEnum.UNREAD.value());
        sysFeedBackReplyEntity.setReplyDate(new Date());
        sysFeedBackReplyEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        insert(sysFeedBackReplyEntity);
        // 更新反馈单状态
        if (sysFeedBackReplyDTO.getStatus() == 1) {
            sysFeedBackService.updateSysFbStatusById(sysFeedBackReplyDTO.getFbId());
        }
    }

    /**
     * 查询用户和客服反馈回复
     *
     * @param fbId 反馈id
     * @return java.util.List<com.softtown.modules.sys.vo.SysFeedBackReplyVO>
     * @author fuming
     * @date 2019/12/2 10:43
     */
    @Override
    public List<SysFeedBackReplyVO> selectFbReplies(Long fbId) {
        List<SysFeedBackReplyEntity> sysFeedBackReplyEntities = baseDao
                .selectFbRepliesByFbIdAndUserType(fbId);
        for (SysFeedBackReplyEntity sysFeedBackReplyEntity : sysFeedBackReplyEntities) {
            // 图片处理
            if (sysFeedBackReplyEntity.getHeadShow() != null) {
                sysFeedBackReplyEntity.setHeadShow(Objects.requireNonNull(OSSFactory.build())
                        .getHeadUrl(sysFeedBackReplyEntity.getHeadShow()));
            }
        }
        return ConvertUtils.sourceToTarget(sysFeedBackReplyEntities, SysFeedBackReplyVO.class);
    }

    /**
     * 查询客服反馈回复id
     *
     * @param fbId 反馈id
     * @return java.util.List<java.lang.Long>
     * @Author fuming
     * @Date 2019/12/6 10:12
     */
    @Override
    public List<Long> selectOprFbRply(Long fbId) {
        return baseDao.selectOprFbRply(fbId);
    }

    /**
     * 更新客服反馈回复的已读状态
     *
     * @param id 反馈回复id
     * @return void
     * @Author fuming
     * @Date 2019/12/6 10:32
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOprRplyReadById(List<Long> id) {
        for (Long ids : id) {
            baseDao.updateOprRplyReadById(ids);
        }
    }

    /**
     * 标识反馈回复表为已读
     *
     * @param fbId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void markRead(Long fbId) {
        // 反馈回复表为已读
        List<SysFeedBackReplyEntity> list =
                baseDao.selectList(new QueryWrapper<SysFeedBackReplyEntity>().lambda().eq(SysFeedBackReplyEntity::getFbId, fbId).eq(SysFeedBackReplyEntity::getUserType, 2).eq(SysFeedBackReplyEntity::getReplyIsRead, 0).eq(SysFeedBackReplyEntity::getIsDeleted, 0));
        if (list != null && !list.isEmpty()) {
            for (SysFeedBackReplyEntity s : list) {
                s.setReplyIsRead(FbIsReadEnum.HAVE_READ.value());
                s.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            }
            this.updateBatchById(list);
        }
    }
}
